﻿namespace RESX_Utils
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using Microsoft.Win32;

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.InitializeComponent();
        }

        private void BrowseRESX_Click(object sender, RoutedEventArgs e)
        {
            var result = this.ShowOpenFileDialog(".resx", "Resource Files (*.resx)|*.resx");
            if (!string.IsNullOrEmpty(result))
            {
                ResourceFile.Text = result;
            }
        }

        private void BrowseResx2_Click(object sender, RoutedEventArgs e)
        {
            var result = this.ShowOpenFileDialog(".resx", "Resource Files (*.resx)|*.resx");
            if (!string.IsNullOrEmpty(result))
            {
                ResourceFile2.Text = result;
             }
        }

        private void BrowseProject_Click(object sender, RoutedEventArgs e)
        {
            var result = this.ShowOpenFileDialog(".", "C# project files (*.csproj)|*.csproj");
            if (!string.IsNullOrEmpty(result))
            {
                this.ProjectFiles.Text = result;
                this.AddToCombobox(this.ProjectFiles, this.ProjectFiles.Text);
            }
        }

        private void RemoveProjectFile_Click(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrEmpty(ProjectFiles.Text))
            {
                this.RemoveFromCombobox(this.ProjectFiles, this.ProjectFiles.Text);
                this.ProjectFiles.Text = string.Empty;
            }
        }

        private void AddProjectFile_Click(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrEmpty(ProjectFiles.Text))
            {
                this.AddToCombobox(this.ProjectFiles, this.ProjectFiles.Text);
            }
        }

        private string ShowOpenFileDialog(string ext, string filter)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.DefaultExt = ext;
            dialog.Filter = filter;

            bool? result = dialog.ShowDialog();
            if (result.HasValue && result.Value)
            {
                return dialog.FileName;
            }

            return string.Empty;
        }

        private void BeforeBackgroundProcess()
        {
            this.SaveSettings();
            this.IsEnabled = false;
            this.ProgressBar.IsIndeterminate = true;
        }

        private void AfterBackgroundProcess(string message, StringBuilder builder)
        {
            this.ProgressBar.IsIndeterminate = false;
            this.IsEnabled = true;

            Clipboard.SetText(builder.ToString());
            MessageBox.Show(message, App.Current.MainWindow.Title, MessageBoxButton.OK, MessageBoxImage.Information);
        }

        private async void Scan_Click(object sender, RoutedEventArgs e)
        {
            // If user forgot to click add.
            if (!string.IsNullOrEmpty(ProjectFiles.Text))
            {
                this.AddToCombobox(this.ProjectFiles, this.ProjectFiles.Text);
            }
            
            if (string.IsNullOrWhiteSpace(ResourceFile.Text) || (ProjectFiles.Items.Count == 0))
            {
                MessageBox.Show("Please specify the resource and project file names.", App.Current.MainWindow.Title, MessageBoxButton.OK, MessageBoxImage.Exclamation);
                return;
            }

            this.BeforeBackgroundProcess();

            var utils = new ResourceUtilities();
            var clipboardBuilder = new StringBuilder();

            var message = await utils.AnalyzeUssage_Async(
                ResourceFile.Text,
                ProjectFiles.Items.Cast<string>(),
                new List<string>(ContentFileExtensions.Text.Split(',')),
                new List<string>(Exclusions.Text.Split(',')),
                clipboardBuilder);

            this.AfterBackgroundProcess(message, clipboardBuilder);
        }

        private async void CompareButton_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(ResourceFile.Text) || string.IsNullOrWhiteSpace(ResourceFile2.Text))
            {
                MessageBox.Show("Please specify the resource and master resource file names.", App.Current.MainWindow.Title, MessageBoxButton.OK, MessageBoxImage.Exclamation);
                return;
            }

            if (ResourceFile.Text.Trim().CompareTo(ResourceFile2.Text.Trim()) == 0)
            {
                MessageBox.Show("Please select two different resource files.", App.Current.MainWindow.Title, MessageBoxButton.OK, MessageBoxImage.Exclamation);
                return;
            }

            this.BeforeBackgroundProcess();

            var utils = new ResourceUtilities();
            var clipboardBuilder = new StringBuilder();

            var message = await utils.CompareResources_Async(ResourceFile.Text, ResourceFile2.Text, clipboardBuilder);

            this.AfterBackgroundProcess(message, clipboardBuilder);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.LoadSettings();
        }

        private async void DetectDuplicates_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(ResourceFile.Text))
            {
                MessageBox.Show("Please specify the resource file name.", App.Current.MainWindow.Title, MessageBoxButton.OK, MessageBoxImage.Exclamation);
                return;
            }

            this.BeforeBackgroundProcess();

            var utils = new ResourceUtilities();
            var clipboardBuilder = new StringBuilder();

            var message = await utils.DetectDuplicates_Async(ResourceFile.Text, clipboardBuilder);

            this.AfterBackgroundProcess(message, clipboardBuilder);
        }

        private void SaveSettings()
        {
            Properties.Settings.Default.ResourceFile = ResourceFile.Text;
            Properties.Settings.Default.MasterResourceFile = ResourceFile2.Text;
            Properties.Settings.Default.ContentFiles = ContentFileExtensions.Text;
            Properties.Settings.Default.Exclusions = Exclusions.Text;
            Properties.Settings.Default.CurrentProjectFile = ProjectFiles.Text;
            Properties.Settings.Default.ProjectFiles = new System.Collections.Specialized.StringCollection();
            foreach (string projectFile in ProjectFiles.Items)
            {
                Properties.Settings.Default.ProjectFiles.Add(projectFile);
            }

            Properties.Settings.Default.Save();
        }

        private void LoadSettings()
        {
            ResourceFile.Text = Properties.Settings.Default.ResourceFile;
            ResourceFile2.Text = Properties.Settings.Default.MasterResourceFile;
            ContentFileExtensions.Text = Properties.Settings.Default.ContentFiles;
            Exclusions.Text = Properties.Settings.Default.Exclusions;
            ProjectFiles.Text = Properties.Settings.Default.CurrentProjectFile;
            if (Properties.Settings.Default.ProjectFiles != null)
            {
                foreach (var projectFile in Properties.Settings.Default.ProjectFiles)
                {
                    ProjectFiles.Items.Add(projectFile);
                }
            }
        }

        private void AddToCombobox(ComboBox cb, string value)
        {
            foreach (string item in cb.Items)
            {
                if (item == value)
                {
                    return;
                }
            }

            ProjectFiles.Items.Add(value);
        }

        private void RemoveFromCombobox(ComboBox cb, string value)
        {
            foreach (string item in cb.Items)
            {
                if (item == value)
                {
                    cb.Items.Remove(item);
                    return;
                }
            }
        }
    }
}
